package com.example.common;

import com.aliyuncs.utils.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.Date;

/**
 * @author itheima
 */
public class CalculationUnits {
    /**
     * 将生日转成年龄
     *
     * @param birthday
     * @return
     */
    public static Long birthdayToAge(Date birthday) {
        if (birthday == null) {
            return 0L;
        }
        //data转LocalDateTime
        LocalDateTime myBirthday = birthday.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        // 本地日期时间对象：此刻的
        LocalDateTime today = LocalDateTime.now();
        long age = ChronoUnit.YEARS.between(myBirthday, today);
        return age;
    }

    /**
     * 将字符串生日，转成年龄
     *
     * @param birthday 生日
     * @return 年龄
     */
    public static Long birthdayToAge(String birthday) {
        if (StringUtils.isEmpty(birthday)) {
            return 0L;
        }

        // 1. 获取当前时间
        LocalDate now = LocalDate.now();

        // 2. 将生日转为可运算的时间
        LocalDate birthdayDate = LocalDate.parse(birthday, DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        // 3. 计算两个时间的间隔
        Period between = Period.between(now, birthdayDate);

        // 4. 获取间隔的年份
        return (long) Math.abs(between.getYears());
    }

    /**
     * 求用户的缘分值
     *
     * @param myAge
     * @param age
     * @return
     */
    public static Long score(Long myAge, Long age) {
        if (myAge == null || age == null) {
            return 0L;
        }
        //获取两个年龄的差值求绝对值
        long ageBetween = Math.abs(myAge - age);
        long score = 100 - (ageBetween * 3);
        //判断结果是否小于0，小于0则取0
        return score > 0 ? score : 0;
    }

    /**
     * 发布时间距离现在的时间间隔
     *
     * @param createTime
     * @return
     */
    public static String timeLag(LocalDateTime createTime) {
        if (createTime == null) {
            return null;
        }
        // 本地日期时间对象：此刻的
        LocalDateTime today = LocalDateTime.now();

        long years = ChronoUnit.YEARS.between(createTime, today);
        if (years > 0) {
            return years + "年前";
        }
        long months = ChronoUnit.MONTHS.between(createTime, today);
        if (months > 0) {
            return months + "月前";
        }
        long weeks = ChronoUnit.WEEKS.between(createTime, today);
        if (weeks > 0) {
            return weeks + "星期前";
        }
        long days = ChronoUnit.DAYS.between(createTime, today);
        if (days > 0) {
            return days + "天前";
        }
        long hours = ChronoUnit.HOURS.between(createTime, today);
        if (hours > 0) {
            return hours + "小时前";
        }
        long minutes = ChronoUnit.MINUTES.between(createTime, today);
        if (minutes > 0) {
            return minutes + "分钟前";
        }
        long seconds = ChronoUnit.SECONDS.between(createTime, today);
        if (seconds > 0) {
            return seconds + "秒前";
        }
        return createTime + "";
    }

    /**
     * input流 转 base64
     * @param is 输入流
     * @return base64字符串
     */
    public static String inputStream2Base64( InputStream is) throws Exception {
        byte[] data = null;
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = is.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new Exception("输入流关闭异常");
                }
            }
        }
        return Base64.getEncoder().encodeToString(data);
    }

    /**
     * base64 转 input流
     * @param base64string base64字符串
     * @return 输入流
     */
    public static InputStream base2InputStream(String base64string) {
        ByteArrayInputStream stream = null;
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] result = decoder.decode(base64string);
            stream = new ByteArrayInputStream(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stream;
    }
}
